/*
 * @Author: liusheng
 * @Date: 2022-06-26 19:40:57
 * @LastEditors: liusheng
 * @LastEditTime: 2022-06-26 19:43:50
 * @Description: 剑指 Offer II 086. 分割回文子字符串
 * email:liusheng613@126.com
 * Copyright (c) 2022 by liusheng/liusheng, All Rights Reserved. 
 * 
 * 剑指 Offer II 086. 分割回文子字符串
给定一个字符串 s ，请将 s 分割成一些子串，使每个子串都是 回文串 ，返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

 

示例 1：

输入：s = "google"
输出：[["g","o","o","g","l","e"],["g","oo","g","l","e"],["goog","l","e"]]
示例 2：

输入：s = "aab"
输出：[["a","a","b"],["aa","b"]]
示例 3：

输入：s = "a"
输出：[["a"]]
 

提示：

1 <= s.length <= 16
s 仅由小写英文字母组成
 

注意：本题与主站 131 题相同： https://leetcode-cn.com/problems/palindrome-partitioning/

通过次数11,327   提交次数15,076
 */

#include "header.h"
class Solution {
public:
    vector<vector<string>> partition(string s) {
        vector<vector<string>> result;
        vector<string> prefix;
        partition(s,0,s.size(),prefix,result);
        return result;
    }
private:
    void partition(string &s,int curIndex,int totalSize,vector<string> & prefix,vector<vector<string>> & result)
    {
        if (curIndex == totalSize)
        {
            result.push_back(prefix);
            return;
        }

        for (int i = curIndex; i < totalSize; ++i)
        {
            if (isPalindrome(s,curIndex,i))
            {
                prefix.push_back(s.substr(curIndex,i-curIndex + 1));
                partition(s,i + 1,totalSize,prefix,result);
                prefix.pop_back();
            }
        }
    }

    bool isPalindrome(string s,int start,int end)
    {
        while (start <= end)
        {
            if (s[start++] != s[end--])
            {
                return false;
            }
        }
        return true;
    }
};
